package com.hdsx.jxcsxm.listener;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author 占贤银
 * time 2024/1/2 15:22
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
@Component
public class QuartzSchedulerListener implements ServletContextListener {

    private ServletContextEvent sce;

    private static WebApplicationContext springContext;

    /**
     *
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Struts 2.1.4 + Quartz 2.2.2：计划下达文号自动更新============>>");
        final ServletContext servletContext = ServletActionContext.getServletContext();

        System.out.println("servletContext：" + servletContext);

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("servletContext-old：" + servletContext);
            }
        };

        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2);
        scheduledThreadPool.schedule(timerTask, 10000, TimeUnit.MILLISECONDS);
        //scheduledThreadPool.scheduleAtFixedRate(timerTask, 10000, 10000, TimeUnit.MILLISECONDS);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

    /*@Override
    public void contextInitialized(ServletContextEvent sce) {
        this.sce = sce;
        final JobDetail jobDetail = JobBuilder.newJob(QuartzSchedulerListener.class)
                .withIdentity("anyJobName", "group1").build();
        try {
            final CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("anyTriggerName", "group1")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/58 * * * * ?"))
                    .build();
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext)
            throws JobExecutionException {
        System.out.println("开始执行==========》》");
        System.out.println(this.sce);
    }*/
}
